package com.example.paymentdemo.task;


import com.example.paymentdemo.entity.OrderInfo;
import com.example.paymentdemo.enums.PayType;
import com.example.paymentdemo.service.AliPayService;
import com.example.paymentdemo.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class AliPayTask {
    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 每30秒执行一次 查询创建超过5mins且未支付的订单 对齐进行校验 如果远程订单未创建则删掉  然后根据远程状态进行订单状态更新 反正不能是未支付
     * @throws Exception
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm() throws Exception {
        log.info("orderConfirm 被执行......");

        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5, PayType.ALIPAY.getType());

        for (OrderInfo orderInfo : orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("超时订单 ===> {}", orderNo);

            //核实订单状态：调用支付宝支付查单接口
            aliPayService.checkOrderStatus(orderNo);
        }
    }
}
